package com.shangxia.leetcode.hoot100.easy.minStack;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;

import java.util.Stack;

/**
 * @ProjectName: leetcode
 * @ClassName: MinStack
 * @Time: 2020/11/29
 * @Author: zhouy
 * @Description:
 * @Experience:
 */

@Slf4j
public class MinStack2 {

    private Stack<Node> xMinStack;

    public MinStack2() {
        xMinStack = new Stack<>();
    }

    public void push(int x) {
        if (ObjectUtils.isEmpty(xMinStack)) {
            xMinStack.push(new Node(x, x));
        } else {
            if (x <= xMinStack.peek().min) {
                xMinStack.push(new Node(x, x));
            } else {
                xMinStack.push(new Node(x, xMinStack.peek().min));
            }
        }
    }

    public void pop() {
        xMinStack.pop();
    }

    public int top() {
        return xMinStack.peek().val;
    }

    public int getMin() {
        return xMinStack.peek().min;
    }

    public static void main(String[] args) {
        MinStack2 minStack = new MinStack2();
        minStack.push(-2);
        minStack.push(0);
        minStack.push(-3);
        System.out.println(minStack.getMin());
        minStack.pop();
        minStack.top();
        System.out.println(minStack.getMin());
    }

    @Data
    @AllArgsConstructor
    class Node {
        int val;
        int min;
    }
}
